const setLine = 4 // 行数
const setWord = 80 //字数
const setRestLine = 1 //剩余空间显示内容的行数
const setRestWord = 10 //剩余空间显示内容的字数
const setMoreSign = '<p class="moreText">>>>全文</p>' //设置发生截取时的字样
export const decodeHTML = (html) => {
  let hasP = html.indexOf('</p>')
  let hasOl = html.indexOf('</ol>')
  if (hasP != -1 && hasOl == -1){
    return handlePureP(html, setLine, setWord).result
  } else if (hasP == -1 && hasOl != -1){
    return handlePureOl(html, setLine, setWord).result
  } else if (hasP != -1 && hasOl != -1){
    let beforeOl = html.split('<ol>')[0]
    if (beforeOl != ''){
      let pRes = handlePureP(beforeOl, setLine, setWord)
      if (pRes.state){ //如果已经发生了截取
        return pRes.result
      }else{
        let firstOl = html.match(/<ol>([\s\S]*?)<\/ol>/)[0]
        return pRes.result + handlePureOl(firstOl, setRestLine, setRestWord).result
      }
    }else{
      let firstOl = html.match(/<ol>([\s\S]*?)<\/ol>/)[0]
      let olRes = handlePureOl(firstOl, setLine, setWord)
      if (olRes.state){
        return olRes.result
      }else{
        let afterOl = html.split('</ol>')[1].match(/<p>([\s\S]*?)<\/p>/)[0]
        return olRes.result + handlePureP(afterOl, setRestLine, setRestWord).result
      }
    }
  }else{
    return ''
  }
}

// 处理只有 p 的 html
const handlePureP = (html, line, word) => {
  let lineCount = 0 //行数
  let wordCount = 0 //字数
  let pTagStart = []
  let result = ''
  let state = false // 状态， 表示传入的内容是否被截取过
  pTagStart = html.split('<p>')
  pTagStart.shift()
  lineCount = pTagStart.length
  if (lineCount > line) {
    pTagStart = pTagStart.slice(0, line)
    state = true
  }
  let lastWordCount = 0
  let sliceList = []
  for (let i = 0; i < pTagStart.length; i++) {
    let deleteTag = pTagStart[i].replace(/<\/?.+?>/g, "")
    wordCount = wordCount + deleteTag.length
    if (wordCount > word) {
      let differenceCount = word - lastWordCount
      sliceList.push(pTagStart[i].substring(0, differenceCount) + '...</p>')
      state = true
      break
    } else {
      lastWordCount = lastWordCount + deleteTag.length
      if ((i == pTagStart.length - 1) && state){
        sliceList.push(pTagStart[i] + '<span>...</span>')
      }else{
        sliceList.push(pTagStart[i])
      }
    }
  }
  pTagStart = sliceList
  pTagStart = pTagStart.map(item => {
    return '<p>' + item
  })
  result = pTagStart.join('')
  if(state){
    result = result + setMoreSign
  }
  return {
    result,
    state
  }
}

// 处理只有 ol 的 html
const handlePureOl = (html, line, word) => {
  let lineCount = 0 //行数
  let wordCount = 0 //字数
  let pTagStart = []
  let result = ''
  let state = false // 状态， 表示传入的内容是否被截取过
  pTagStart = html.match(/<ol>([\s\S]*?)<\/ol>/)[1].split('<li>')
  pTagStart.shift()
  if (pTagStart.length > line) {
    pTagStart = pTagStart.slice(0, line)
    state = true
  }
  let lastWordCount = 0
  let sliceList = []
  for (let i = 0; i < pTagStart.length; i++) {
    let deleteTag = pTagStart[i].replace(/<\/?.+?>/g, "")
    wordCount = wordCount + deleteTag.length
    if (wordCount > word) {
      let differenceCount = word - lastWordCount
      sliceList.push(pTagStart[i].substring(0, differenceCount) + '...</li>')
      state = true
      break
    } else {
      lastWordCount = lastWordCount + deleteTag.length
      if (i == pTagStart.length - 1 && state) {
        sliceList.push(pTagStart[i] + '<span>...</span>')
      } else {
        sliceList.push(pTagStart[i])
      }
    }
  }
  pTagStart = sliceList
  pTagStart = pTagStart.map(item => {
    return '<li>' + item
  })
  result = '<ol>' + pTagStart.join('') + '</ol>'
  if (state) {
    result = result + setMoreSign
  }
  return {
    result,
    state
  }
}